<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

require_once (_PS_CLASS_DIR_ . 'FrontController.php');
include_once(dirname(__FILE__)."/../Zadmindepartment.php");
include_once(dirname(__FILE__)."/../ZadminPriority.php");
include_once(dirname(__FILE__)."/../classes/ZticketObject.php");

class ZhelpdeskTicketController extends FrontControllerCore {

    public function setMedia() {
    	parent::setMedia();
    	Tools::addCSS(_THEME_CSS_DIR_.'history.css');
    }

	/**
     * see parent
     * Change get n default 
     */
	public function pagination($nbProducts = 10)
	{
		if (!self::$initialized)
			$this->init();

		$nArray = (int)(Configuration::get('PS_PRODUCTS_PER_PAGE')) != 10 ? array((int)(Configuration::get('PS_PRODUCTS_PER_PAGE')), 10, 20, 50) : array(10, 20, 50);
		asort($nArray);
		$this->n = abs((int)(Tools::getValue('n', ((isset(self::$cookie->nb_item_per_page) AND self::$cookie->nb_item_per_page >= 10) ? self::$cookie->nb_item_per_page : (int)(Configuration::get('ZHD_NB_LIST_TICKET'))))));
		$this->p = abs((int)(Tools::getValue('p', 1)));
		
		$current_url = tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']);
		//delete parameter page
		$current_url = preg_replace('/(\?)?(&amp;)?p=\d+/', '$1', $current_url);
		
		$range = 2; /* how many pages around page selected */

		if ($this->p < 0)
			$this->p = 0;

		if (isset(self::$cookie->nb_item_per_page) AND $this->n != self::$cookie->nb_item_per_page AND in_array($this->n, $nArray))
			self::$cookie->nb_item_per_page = $this->n;

		if ($this->p > ($nbProducts / $this->n))
			$this->p = ceil($nbProducts / $this->n);
		$pages_nb = ceil($nbProducts / (int)($this->n));

		$start = (int)($this->p - $range);
		if ($start < 1)
			$start = 1;
		$stop = (int)($this->p + $range);
		if ($stop > $pages_nb)
			$stop = (int)($pages_nb);
		self::$smarty->assign('nb_products', $nbProducts);
		$pagination_infos = array(
			'products_per_page' => (int)Configuration::get('PS_PRODUCTS_PER_PAGE'),
			'pages_nb' => $pages_nb,
			'p' => $this->p,
			'n' => $this->n,
			'nArray' => $nArray,
			'range' => $range,
			'start' => $start,
			'stop' => $stop,
			'current_url' => $current_url
		);
		self::$smarty->assign($pagination_infos);
	}
    
    /**
     * display content
     */
    public function displayContent() {
    	$id_customer = (!$id_customer = (int)self::$cookie->id_customer) ? false : (int)self::$cookie->id_customer;
    	if ($id_customer != false) {
    		$nbTicket=ZTicketObject::getNumberTicket($id_customer);
    		$this->pagination ($nbTicket);
	    	$tickets = ZTicketObject::getAllTicket ($id_customer,self::$cookie->id_lang,$this->p,$this->n,'date_add','DESC');
	    	self::$smarty->assign(array(
				'tickets'=> $tickets,
	    		'nb_products' => $nbTicket,
			));
	        parent::displayContent();
	        self::$smarty->display(_PS_MODULE_DIR_ . 'zhelpdesk/themes/tickets.tpl');
    	}
    }    
}
